#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,a[500010],dp[2010],ans,mc[500010],stk[500010],hed=1,tal;
const int mod=1e9+7;
signed main(){
	freopen("knight.in","r",stdin);
	freopen("knight.out","w",stdout);
	scanf("%lld%lld",&n,&k);
	mc[0]=1;
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
		mc[i]=mc[i-1]*23,mc[i]%=mod;
	}
	if(n<=2000){
		for(int i=1;i<=n;i++){
			dp[i]=1e18;
			int now=a[i];
			for(int j=i-1;j>=max(0ll,i-k);j--)
				dp[i]=min(dp[i],now+dp[j]),now=max(now,a[j]);
			ans+=mc[n-i]*dp[i]%mod,ans%=mod;
		}
		printf("%lld",ans);
	}else{
		stk[++tal]=0;
		for(int i=1;i<=n;i++){
			while(stk[hed]<i-k&&hed<tal) hed++;
			dp[i]=dp[stk[hed]]+a[i];
			while(dp[i]<dp[stk[tal]]&&tal>=hed) tal--;
			stk[++tal]=i;
			ans+=mc[n-i]*dp[i]%mod,ans%=mod;
		}
		printf("%lld",ans);
	}
	return 0;
}
